package cn.jcet.mapreduce.flowsum;

import cn.jcet.mapreduce.bean.FlowBean;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

/**
 * @author 鲜磊
 * @Date 2020/12/3 0:29
 */
public class FlowCountReduce extends Reducer<Text,FlowBean,Text,FlowBean> {

    // 创建对象
    private FlowBean v = new FlowBean();

    @Override
    protected void reduce(Text key, Iterable<FlowBean> values, Context context) throws IOException, InterruptedException {
        // 数据累加求和,初始值必须为0
        long sum_upFlow = 0;
        long sum_downFlow = 0;
        // 对values进行遍历
        for (FlowBean flowBean : values) {
            sum_upFlow += flowBean.getUpFlow();
            sum_downFlow += flowBean.getDownFlow();
        }
        // 设置求和数据
        v.set(sum_upFlow,sum_downFlow);
        // 写出
        context.write(key,v);
    }


}
